﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Tab2Sql.Classes;

namespace Tab2Sql
{
    /// <summary>
    /// Text to SQL Statements. The entire process, from delimited columnar text to SQL Insert statements.
    /// </summary>
    public class TSV2SQL
    {
        private StringBuilder theStatements;
        private StringBuilder theErrors;
        private string tableName;
        /// <summary>
        /// Has the job failed? Status 0 = It's all good.
        /// </summary>
        public int StatusCode;

        /// <summary>
        /// Main constructor. SQLVariant allows some control over SQL flavour produced although compatability is not guaranteed.
        /// </summary>
        /// <param name="inputText"></param>
        /// <param name="DesiredVariant"></param>
        public TSV2SQL(string inputText, SqlVariants DesiredVariant)
        {
            theErrors = new StringBuilder();
            theStatements = new StringBuilder();
            StatusCode = 0;
            Tab2Sql.Classes.Core.MainTab2Sql mts = new Tab2Sql.Classes.Core.MainTab2Sql(inputText, new Options());
            tableName = mts.OurDataTable.TableName;
            
            theStatements.Append(mts.FullSql);

        }


        /// <summary>
        /// Constructor. SQLVariant is assumed to be TSql.
        /// </summary>
        /// <param name="inputText"></param>
        public TSV2SQL(string inputText)
        {
            theStatements = new StringBuilder();

            StatusCode = 0;
            //TSV2DataTable tdt = new TSV2DataTable(inputText, new Tab2sqlOptions(), new Tab2SqlObj());
            
           // DataTable2SQL dts = new DataTable2SQL(tdt.Dt);
            
            //theStatements.Append(dts.ToString());

            
        }
        /// <summary>
        /// Table name as used in output statements and internal management of object.
        /// </summary>
        public string TableName
        {
            get
            {
                if (tableName == null)
                {
                    return "UNSET";
                }
                else
                {
                    return tableName;
                }
            }
            set { tableName = value; }
        }
        /// <summary>
        /// Outputs sql statements unless there' been an error in which case outputs debug log.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (StatusCode == 0)
            {
                return theStatements.ToString();
            }
            else
            {
                return theErrors.ToString();
            }
        }


    }
}
